﻿(function($) {
    $.widget("ui.rioCmb", {
        options : {
            'selected' : -1
        },

        _create : function() {
            var $self = this;
            var element = $self.element;
            element.addClass('rio-cmb');
            var s = $self.s = element.find('.cmb-selected a');
            var wrap = element.find('.cmb-options');
            
            element.bind('mouseenter', function(event){
                wrap.show();
            }).bind('mouseleave', function(event){
                wrap.hide();
            });
            
            element.find('.cmb-options a').each(function(_index, _item){
                $(_item).bind('click.rio', function(event){
                    var v = $(_item).attr('value');
                    var n = $(_item).html();
                    s.attr('value', v);
                    s.html(n);
                    $self.options.selected = v;
                    wrap.hide();
                })
            });
            // 设置options
            $self._setOptions($self.options);
        },
        _setOption : function(key, value) {
            var $self = this;
            var left = (value - 140) * 5 + 45;
            switch (key) {
            case 'selected':
                if(value == ''){
                    value = -1;
                    $self.options.selected = value;
                }
                $self.element.find('.cmb-options a').each(function(_index, _item){
                    var v = $(_item).attr('value');
                    var n = $(_item).html();
                    if(v == value){
                        $self.s.attr('value', v);
                        $self.s.html(n);
                    }
                });
                break;
            default:
                break;
            }
            return this;
        }
    });

})(jQuery);
